package de.com.cn.springboot.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import de.com.cn.springboot.common.JsonResult;
import de.com.cn.springboot.dao.MenuMapper;
import de.com.cn.springboot.dao.UserMapper;
import de.com.cn.springboot.model.Menu;
import de.com.cn.springboot.model.User;


@Controller
public class LoginController {
	
	@Autowired
	private UserMapper usermapper;
	
	@Autowired
	private MenuMapper menuMapper;
	
	
	@RequestMapping(path = {"/login"})
    public String HelloSpring (){
        System.out.println("hello spring boot");
        return "login";
    }
    
    @RequestMapping(path = {"/login"},method=RequestMethod.POST)
    @ResponseBody
	public JsonResult login(@ModelAttribute User user) {
		JsonResult result = JsonResult.createSuccess();
		System.out.println("登陆账号:"+user.getAccount()+"==密码:"+user.getPassword());
		User bean = usermapper.selectByPrimaryKey(user);
		System.out.println("登陆账号:"+bean.getAccount()+"==密码:"+bean.getPassword());
		
		result.setCode(1);
		result.setMsg("登陆成功！");
		return result;
	}
    
    @RequestMapping(path = {"/menu"},method=RequestMethod.GET)
    @ResponseBody
	public JsonResult menu() {
		JsonResult result = JsonResult.createSuccess();
		Map<String, Object> map =new HashMap<String, Object>();
		map.put("parentid", 0);
		List<Menu> selectList = zuhemenu(menuMapper.selectList(map));
		
		result.addData(selectList);
		result.setCode(1);
		result.setMsg("查询成功！");
		return result;
	}
    
    public List<Menu> zuhemenu(List<Menu> selectList){
    	List<Menu> newlist = new ArrayList<>();
    	Map<String, Object> map =new HashMap<String, Object>();
		
    	for (Menu menu : selectList) {
			if(menu.getLaststage()==1) {
				map.put("parentid", menu.getId());
				List<Menu> sonmenu = menuMapper.selectList(map);
				List<Menu> zuhemenu = zuhemenu(sonmenu);
				menu.setSonmenu(zuhemenu);
			}
		}
    	
		return selectList;
    }
    
    @RequestMapping(path = {"/index"})
    public String index (){
        System.out.println("index");
        return "index/index";
    }
    
    @RequestMapping(path = {"/frist"})
    public String fist (){
        System.out.println("frist");
        return "index/frist";
    }
}
